//#include "Driver_sdcard.h"
#include "driver_Mobvoi_includes.h"
#include "driver_sdcard.h"
#include "diskio.h"
#if 1 // (CONFIG_APP_MP3PLAYER == 1)
enum
{
    DISK_TYPE_SD = 0,
    DISK_TYPE_UDISK
};
static uint8 cur_disk_type = DISK_TYPE_SD;

DSTATUS disk_initialize(uint8 pdrv)
{
    os_printf("disk initialize--- \r\n");
#ifdef CONFIG_APP_SDCARD
    int cnt = 5;
#endif
    cur_disk_type = pdrv;
    if (pdrv == DISK_TYPE_SD)
    {
#ifdef CONFIG_APP_SDCARD
        while (cnt--)
        {
            if (SD_init() == SD_OK)
                return RES_OK;
            os_printf("SD init retry cnt = %d\r\n", cnt);
            Delay(50);
        }
#endif
    }
    else
    {
#ifdef CONFIG_APP_USB
        if (udisk_init() == USB_RET_OK)
            return RES_OK;
#endif
    }
    return STA_NOINIT;
}

DSTATUS disk_status(BYTE pdrv)
{
    //test
    return RES_OK;
}

DRESULT disk_read(
    BYTE pdrv, /* Physical drive nmuber to identify the drive */
    uint8 *buff,
    DWORD sector,
    UINT count)
{
#ifdef CONFIG_APP_SDCARD
    int res;
    uint8 read_loop = 30; // try more times, to advoid read error
#endif

    //if(!Media_is_online())
    //    return RES_NOMEDIA;

    if (cur_disk_type == DISK_TYPE_SD)
    {
#ifdef CONFIG_APP_SDCARD
        while (read_loop--)
        {
            res = sdcard_read_multi_block(buff, sector, count);
            if (res == SD_OK)
            {
                return RES_OK;
            }
            else
            {
                os_printf("read error\r\n", res);
                if (!sd_is_attached())
                {
                    return RES_ERROR;
                }
                Delay(100);
                continue;
            }
        }
#endif
        return RES_ERROR;
    }
    else
    {
#ifdef CONFIG_APP_USB
        if (udisk_rd_blk_sync(sector, count, buff) != USB_RET_OK)
#endif
            return RES_ERROR;
    }
    return RES_OK;
}

DRESULT disk_write(
    BYTE pdrv, /* Physical drive nmuber to identify the drive */
    uint8 *buff,
    DWORD sector,
    UINT count)
{
#ifdef CONFIG_APP_SDCARD
    int res;
    //uint8 read_loop = 30; // try more times, to advoid read error
#endif
#ifdef CONFIG_APP_SDCARD
    if (!count)
        return RES_PARERR;

    SDCARD_PRT("disk write\r\n");

    res = sdcard_write_multi_block((uint8 *)buff, sector, count);
    if (res == SD_OK)
    {
        SDCARD_PRT("sd write ok \r\n");
        return RES_OK;
    }
    else
    {
        SDCARD_FATAL("sd write error:res=%d\r\n", res);
        if (!sd_is_attached())
        {
            return RES_ERROR;
        }
        Delay(100);
    }

    if (res == 0x00)
    {
        return RES_OK;
    }
    else
    {
        return RES_ERROR;
    }

#else
    return RES_OK;
#endif
}

DRESULT disk_ioctl(
    BYTE drv,
    BYTE ctrl, /* Control code */
    void *buff /* Buffer to send/receive control data */
)
{
    return RES_OK;
}

DRESULT disk_unmount(uint8 pdrv)
{
    if (pdrv == DISK_TYPE_SD)
    {
#ifdef CONFIG_APP_SDCARD
        sdcard_uninitialize();
#endif
    }
    else
    {
#ifdef CONFIG_APP_USB
        udisk_uninit();
#endif
    }
    return RES_OK;
}

uint32 get_disk_size(void)
{
    if (cur_disk_type == DISK_TYPE_SD)
    {
#ifdef CONFIG_APP_SDCARD
        return sdcard_get_size();
#else
        return 0;
#endif
    }
    else
    {
#ifdef CONFIG_APP_USB
        return udisk_get_size();
#else
        return 0;
#endif
    }
}

uint8 Media_is_online(void)
{
    if (cur_disk_type == DISK_TYPE_SD)
    {
#ifdef CONFIG_APP_SDCARD
        return sd_is_attached();
#else
        return 0;
#endif
    }
    else
    {
#ifdef CONFIG_APP_USB
        return udisk_is_enumerated();
#else
        return 0;
#endif
    }
}
uint8 get_cur_media_type(void)
{
    return cur_disk_type;
}
#endif
